package javabook;

import com.gule.gl.leetcode.editor.cn.definition.TreeNode;

import java.util.Objects;

/**
 * @Auther: gl
 * @Date: 2020/2/12 12:21
 * @Description:
 */
public class MaxNumFromBTree {
    int max = Integer.MIN_VALUE;
    TreeNode maxTreeNode = null;

    /**
     * 获取二叉树中最大的子节点累加求和，运用后续遍历
     *
     * @param tree
     * @param maxTreeNode
     * @return
     */
    public int getMaxNumFromTree(TreeNode tree, TreeNode maxTreeNode) {
        if (Objects.isNull(tree)) {
            return 0;
        }
        int maxLeftNum = getMaxNumFromTree(tree.left, maxTreeNode);
        int maxRightNum = getMaxNumFromTree(tree.left, maxTreeNode);

        int sum = maxLeftNum + maxRightNum + tree.val;
        if (sum > max) {
            max = sum;
            maxTreeNode = tree;
        }
        return sum;
    }
}
